﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;

namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class ServerCheckPowerPlan : IssueTypeCheckBase
    {
        public ServerCheckPowerPlan()
        {
            IssueTypeId = 1004;
        }

        public override void Check()
        {
            try
            {
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);
                Issue issue;

                var ServerQry = from s in dc.Servers
                                //from el in s.EventLogs
                                //select el.Server;
                                from pp in s.PowerPlans
                                where pp.isActive == true && !pp.Name.ToLower().Contains("high performance")
                                select s;

                // int prevserv = 0;

                foreach (var item in ServerQry)
                {


                    //Get Issuetype object

                    //Get Issuetype object  And Create a new  Global issue object 

                    IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                    issue = new Issue();

                    issue.IssueType = issueType;
                    issue.TargetId = item.ServerID;
                    issue.Description = issueType.Description.Replace("{ServerName}", item.Name);

                    dc.Issues.InsertOnSubmit(issue);


                }

                dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }

        }


    }








}
